﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using p2p.File.FileLayout.parts;

namespace p2p.Classes
{
    public class CreateTreeView
    {
        private System.Windows.Forms.TreeView treeView;
        private FileList fileList;

        public System.Windows.Forms.TreeView TreeView
        {
            set{
                treeView = value;
            }
        }

        public FileList FileList
        {
            set
            {
                fileList = value;
            }
        }

        public void BuildTreeView(System.Windows.Forms.TreeView TreeView)
        {
            treeView = TreeView;
            buildTreeView(treeView.Nodes);
        }

        public void BuildTreeView(FileList FileList)
        {
            fileList = FileList;
            buildTreeView(treeView.Nodes);
        }

        public void BuildTreeView(System.Windows.Forms.TreeView TreeView, FileList FileList)
        {
            treeView = TreeView;
            fileList = FileList;
            buildTreeView(treeView.Nodes);
        }

        public void BuildTreeView(FileList FileList, System.Windows.Forms.TreeView TreeView)
        {
            treeView = TreeView;
            fileList = FileList;
            buildTreeView(treeView.Nodes);
        }

        public void BuildTreeView()
        {
            buildTreeView(treeView.Nodes);
        }
               

        private void buildTreeView(TreeNodeCollection nodes)
        {   
            nodes.Clear();
            if (fileList.Directory != null && fileList.Directory.Length > 0)
            {
                // go though the directorys
                foreach (Dir Dir in fileList.Directory)
                {
                    // create a new node
                    TreeNode cNode = new TreeNode(Dir.Name);
                    // auto maticly check all files
                    cNode.Checked = true;
                    // check for child directorys and files in directory and add them to the main node
                    nodes.Add(buildDirectory(cNode, Dir));
                }
            }
            if (fileList.Files != null && fileList.Files.Length > 0)
            {
                // go though them one by one
                foreach (p2p.File.FileLayout.parts.File file in fileList.Files)
                {
                    // create a new Node for this Directory
                    TreeNode cNode = new TreeNode(file.Name);
                    // auto maticly check all files
                    cNode.Checked = true;
                    // add the files to the main nodes
                    nodes.Add(cNode);
                }   
            }
        }


        private TreeNode buildDirectory(TreeNode Node, Dir Dir)
        {
            // check that there is some directorys
            if (Dir.Directory != null && Dir.Directory.Length > 0)
            {
                // go though them one by one
                foreach (Dir cDir in Dir.Directory)
                {
                    // create a new Node for this Directory
                    TreeNode cNode = new TreeNode(cDir.Name);
                    // auto maticly check all files
                    cNode.Checked = true;
                    // add any child directorys and files to the parent node
                    Node.Nodes.Add(buildDirectory(cNode, cDir));
                }
            }
            if (Dir.Files != null && Dir.Files.Length > 0)
            {
                // go though them one by one
                foreach (p2p.File.FileLayout.parts.File file in Dir.Files)
                {
                    // create a new Node for this Directory
                    TreeNode cNode = new TreeNode(file.Name);
                    // auto maticly check all files
                    cNode.Checked = true;
                    // add the nodes to the parent node
                    Node.Nodes.Add(cNode);
                }
            }
            return Node;
        }
    }
}
